

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/xt-blog/img/favicon.png">
  <link rel="icon" href="/xt-blog/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="xue tao">
  <meta name="keywords" content="">
  
    <meta name="description" content="难度：中等             题目给定一个变量对数组 equations 和一个实数值数组 values 作为已知条件，其中 equations[i] &#x3D; [Ai, Bi] 和 values[i] 共同表示等式 Ai &#x2F; Bi &#x3D; values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。 另有一些以数组 queries 表示的问题，其中 querie">
<meta property="og:type" content="article">
<meta property="og:title" content="111_计算除法">
<meta property="og:url" content="https://accept_one_time.gitee.io/xt-blog/2022/04/01/111-%E8%AE%A1%E7%AE%97%E9%99%A4%E6%B3%95/index.html">
<meta property="og:site_name" content="学涛的博客">
<meta property="og:description" content="难度：中等             题目给定一个变量对数组 equations 和一个实数值数组 values 作为已知条件，其中 equations[i] &#x3D; [Ai, Bi] 和 values[i] 共同表示等式 Ai &#x2F; Bi &#x3D; values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。 另有一些以数组 queries 表示的问题，其中 querie">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-04-01T08:17:54.000Z">
<meta property="article:modified_time" content="2022-04-01T12:54:39.302Z">
<meta property="article:author" content="xue tao">
<meta property="article:tag" content="leetcode">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>111_计算除法 - 学涛的博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/xt-blog/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="/xt-blog/css/mac.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"accept_one_time.gitee.io","root":"/xt-blog/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/xt-blog/local-search.xml"};
  </script>
  <script  src="/xt-blog/js/utils.js" ></script>
  <script  src="/xt-blog/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/xt-blog/">
      <strong>学涛的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/xt-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/xt-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                文档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/xt-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/xt-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/xt-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/xt-blog/img/default.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="111_计算除法">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-04-01 16:17" pubdate>
        2022年4月1日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.9k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      42 分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">111_计算除法</h1>
            
            <div class="markdown-body">
              <div class="note note-warning">
            <p>难度：中等</p>
          </div>

<h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><p>给定一个变量对数组 equations 和一个实数值数组 values 作为已知条件，其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。</p>
<p>另有一些以数组 queries 表示的问题，其中 queries[j] = [Cj, Dj] 表示第 j 个问题，请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。</p>
<p>返回 所有问题的答案 。如果存在某个无法确定的答案，则用 -1.0 替代这个答案。如果问题中出现了给定的已知条件中没有出现的字符串，也需要用 -1.0 替代这个答案。</p>
<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><p>示例一：</p>
<blockquote>
<p><strong>输入</strong>：equations = [[“a”,”b”],[“b”,”c”]], values = [2.0,3.0], queries = [[“a”,”c”],[“b”,”a”],[“a”,”e”],[“a”,”a”],[“x”,”x”]]<br><strong>输出</strong>：[6.00000,0.50000,-1.00000,1.00000,-1.00000]<br><strong>解释</strong>：<br>条件：a / b = 2.0, b / c = 3.0<br>问题：a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?<br>结果：[6.0, 0.5, -1.0, 1.0, -1.0 ]</p>
</blockquote>
<p>示例二：</p>
<blockquote>
<p><strong>输入</strong>：equations = [[“a”,”b”],[“b”,”c”],[“bc”,”cd”]], values = [1.5,2.5,5.0], queries = [[“a”,”c”],[“c”,”b”],[“bc”,”cd”],[“cd”,”bc”]]<br><strong>输出</strong>：[3.75000,0.40000,5.00000,0.20000]</p>
</blockquote>
<p>示例三：</p>
<blockquote>
<p><strong>输入</strong>：equations = [[“a”,”b”]], values = [0.5], queries = [[“a”,”b”],[“b”,”a”],[“a”,”c”],[“x”,”y”]]<br><strong>输出</strong>：[0.50000,2.00000,-1.00000,-1.00000]</p>
</blockquote>
<h2 id="提示"><a href="#提示" class="headerlink" title="提示"></a>提示</h2><ul>
<li>1 &lt;= equations.length &lt;= 20</li>
<li>equations[i].length == 2</li>
<li>1 &lt;= Ai.length, Bi.length &lt;= 5</li>
<li>values.length == equations.length</li>
<li>0.0 &lt; values[i] &lt;= 20.0</li>
<li>1 &lt;= queries.length &lt;= 20</li>
<li>queries[i].length == 2</li>
<li>1 &lt;= Cj.length, Dj.length &lt;= 5</li>
<li>Ai, Bi, Cj, Dj 由小写英文字母与数字组成</li>
</ul>
<h2 id="解题"><a href="#解题" class="headerlink" title="解题"></a>解题</h2><p>创建一个用来表示边的类 Edge</p>
<figure class="highlight java"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Edge</span></span>&#123;<br>  Map&lt;String, Double&gt; nexts;<br><br>  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Edge</span><span class="hljs-params">()</span></span>&#123;<br>    nexts = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();<br>    Set&lt;Map.Entry&lt;String, Double&gt;&gt; entries = nexts.entrySet();<br>  &#125;<br><br>  <span class="hljs-keyword">public</span> Set&lt;Map.Entry&lt;String, Double&gt;&gt; getNextsSet()&#123;<br>    <span class="hljs-keyword">return</span> nexts.entrySet();<br>  &#125;<br>  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addEdge</span><span class="hljs-params">(String next, <span class="hljs-keyword">double</span> cost)</span></span>&#123;<br>    nexts.put(next, cost);<br>  &#125;<br>  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">canReach</span><span class="hljs-params">(String to)</span></span>&#123;<br>    <span class="hljs-keyword">return</span> nexts.containsKey(to);<br>  &#125;<br>  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">get</span><span class="hljs-params">(String key)</span></span>&#123;<br>    <span class="hljs-keyword">return</span> nexts.get(key);<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>首先对 equations 进行遍历，将 a/b 和 b/a 存储起来</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; values.length; i++) &#123;<br>  List&lt;String&gt; list = equations.get(i);<br>  <span class="hljs-comment">// 如果 edgeMap 不包含当前 equation，则添加</span><br>  <span class="hljs-keyword">if</span> (!edgeMap.containsKey(list.get(<span class="hljs-number">0</span>)))&#123;<br>    edgeMap.put(list.get(<span class="hljs-number">0</span>),<span class="hljs-keyword">new</span> Edge());<br>  &#125;<br>  <span class="hljs-comment">// 添加 a-&gt;b    (a,[b,cost])</span><br>  edgeMap.get(list.get(<span class="hljs-number">0</span>)).addEdge(list.get(<span class="hljs-number">1</span>),values[i]);<br>  <span class="hljs-keyword">if</span> (!edgeMap.containsKey(list.get(<span class="hljs-number">1</span>)))&#123;<br>    edgeMap.put(list.get(<span class="hljs-number">1</span>),<span class="hljs-keyword">new</span> Edge());<br>  &#125;<br>  <span class="hljs-comment">// 添加 b-&gt;a    (b,[a,1/cost])</span><br>  edgeMap.get(list.get(<span class="hljs-number">1</span>)).addEdge(list.get(<span class="hljs-number">0</span>),<span class="hljs-number">1</span>/values[i]);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>定义 dfs 函数查找答案。flags用来记录是否访问过。</p>
<ol>
<li>如果 start 等于 end，表示 start / start，返回 1 </li>
<li>通过 Edge 的 canReach 方法判断是否可以直达<ol>
<li>如果可以，直接返回结果</li>
<li>不可以的话将所有结果列出来，如果已经访问过则跳过，没有访问过则向 flags 里添加当前结果，递归当前结果 <code>double res = dfs(entry.getKey(), end, flags, edgeMap);</code>，结果不为 -1 表示可达，在 edgeMap 里添加当前结果，当前结果表示为 res * entry.getValue()。从 flags 里移除当前访问项。</li>
</ol>
</li>
<li>如果都不符合直接返回 -1</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">dfs</span><span class="hljs-params">(String start, String end, Set&lt;String&gt; flags, Map&lt;String, Edge&gt; edgeMap)</span></span>&#123;<br>  <span class="hljs-keyword">if</span> (start.equals(end)) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>  Edge edge = edgeMap.get(start);<br>  <span class="hljs-keyword">if</span> (edge.canReach(end))&#123;<br>    <span class="hljs-keyword">return</span> edge.get(end);<br>  &#125;<span class="hljs-keyword">else</span> &#123;<br>    <span class="hljs-keyword">for</span> (Map.Entry&lt;String, Double&gt; entry: edge.getNextsSet())&#123;<br>      <span class="hljs-keyword">if</span> (flags.contains(entry.getKey())) <span class="hljs-keyword">continue</span>;<br>      flags.add(entry.getKey());<br>      <span class="hljs-keyword">double</span> res = dfs(entry.getKey(), end, flags, edgeMap);<br>      <span class="hljs-keyword">if</span>(res!=-<span class="hljs-number">1</span>)&#123;<br>        edge.addEdge(end, entry.getValue()*res);<br>        <span class="hljs-keyword">return</span> entry.getValue()*res;<br>      &#125;<br>      flags.remove(entry.getKey());<br>    &#125;<br>  &#125;<br>  <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>&#125;<br></code></pre></td></tr></table></figure>



<p>最终题解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span>[] calcEquation(List&lt;List&lt;String&gt;&gt; equations, <span class="hljs-keyword">double</span>[] values, List&lt;List&lt;String&gt;&gt; queries)&#123;<br>        <span class="hljs-comment">// a, a 可达的点及距离</span><br>        Map&lt;String, Edge&gt; edgeMap = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; values.length; i++) &#123;<br>            List&lt;String&gt; list = equations.get(i);<br>            <span class="hljs-comment">// 如果 edgeMap 不包含当前 equation，则添加</span><br>            <span class="hljs-keyword">if</span> (!edgeMap.containsKey(list.get(<span class="hljs-number">0</span>)))&#123;<br>                edgeMap.put(list.get(<span class="hljs-number">0</span>),<span class="hljs-keyword">new</span> Edge());<br>            &#125;<br>            <span class="hljs-comment">// 添加 a-&gt;b    (a,[b,cost])</span><br>            edgeMap.get(list.get(<span class="hljs-number">0</span>)).addEdge(list.get(<span class="hljs-number">1</span>),values[i]);<br>            <span class="hljs-keyword">if</span> (!edgeMap.containsKey(list.get(<span class="hljs-number">1</span>)))&#123;<br>                edgeMap.put(list.get(<span class="hljs-number">1</span>),<span class="hljs-keyword">new</span> Edge());<br>            &#125;<br>            <span class="hljs-comment">// 添加 b-&gt;a    (b,[a,1/cost])</span><br>            edgeMap.get(list.get(<span class="hljs-number">1</span>)).addEdge(list.get(<span class="hljs-number">0</span>),<span class="hljs-number">1</span>/values[i]);<br>        &#125;<br>        <span class="hljs-keyword">double</span>[] ans = <span class="hljs-keyword">new</span> <span class="hljs-keyword">double</span>[queries.size()];<br>        <span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span> (List&lt;String&gt; query: queries)&#123;<br>            <span class="hljs-keyword">if</span> (!edgeMap.containsKey(query.get(<span class="hljs-number">0</span>))) ans[i++]=-<span class="hljs-number">1</span>;<br>            <span class="hljs-keyword">else</span> ans[i++] = dfs(query.get(<span class="hljs-number">0</span>),query.get(<span class="hljs-number">1</span>),<span class="hljs-keyword">new</span> HashSet&lt;&gt;(),edgeMap);<br>        &#125;<br>        <span class="hljs-keyword">return</span> ans;<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">dfs</span><span class="hljs-params">(String start, String end, Set&lt;String&gt; flags, Map&lt;String, Edge&gt; edgeMap)</span></span>&#123;<br>        <span class="hljs-keyword">if</span> (start.equals(end)) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>        Edge edge = edgeMap.get(start);<br>        <span class="hljs-keyword">if</span> (edge.canReach(end))&#123;<br>            <span class="hljs-keyword">return</span> edge.get(end);<br>        &#125;<span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">for</span> (Map.Entry&lt;String, Double&gt; entry: edge.getNextsSet())&#123;<br>                <span class="hljs-keyword">if</span> (flags.contains(entry.getKey())) <span class="hljs-keyword">continue</span>;<br>                flags.add(entry.getKey());<br>                <span class="hljs-keyword">double</span> res = dfs(entry.getKey(), end, flags, edgeMap);<br>                <span class="hljs-keyword">if</span>(res!=-<span class="hljs-number">1</span>)&#123;<br>                    edge.addEdge(end, entry.getValue()*res);<br>                    <span class="hljs-keyword">return</span> entry.getValue()*res;<br>                &#125;<br>                flags.remove(entry.getKey());<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    &#125;<br>    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Edge</span></span>&#123;<br>        Map&lt;String, Double&gt; nexts;<br><br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Edge</span><span class="hljs-params">()</span></span>&#123;<br>            nexts = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();<br>            Set&lt;Map.Entry&lt;String, Double&gt;&gt; entries = nexts.entrySet();<br>        &#125;<br><br>        <span class="hljs-keyword">public</span> Set&lt;Map.Entry&lt;String, Double&gt;&gt; getNextsSet()&#123;<br>            <span class="hljs-keyword">return</span> nexts.entrySet();<br>        &#125;<br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addEdge</span><span class="hljs-params">(String next, <span class="hljs-keyword">double</span> cost)</span></span>&#123;<br>            nexts.put(next, cost);<br>        &#125;<br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">canReach</span><span class="hljs-params">(String to)</span></span>&#123;<br>            <span class="hljs-keyword">return</span> nexts.containsKey(to);<br>        &#125;<br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">get</span><span class="hljs-params">(String key)</span></span>&#123;<br>            <span class="hljs-keyword">return</span> nexts.get(key);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/xt-blog/categories/Leetcode/">Leetcode</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/xt-blog/tags/leetcode/">leetcode</a>
                    
                  </div>
                
              </div>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/xt-blog/2022/04/06/765-%E6%83%85%E4%BE%A3%E7%89%B5%E6%89%8B/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">765_情侣牵手</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/xt-blog/2022/03/29/2024-%E8%80%83%E8%AF%95%E7%9A%84%E6%9C%80%E5%A4%A7%E5%9B%B0%E6%89%B0%E5%BA%A6/">
                        <span class="hidden-mobile">2024_考试的最大困扰度</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     Peace <i class="iconfont icon-love"></i> Love 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/xt-blog/js/events.js" ></script>
<script  src="/xt-blog/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/xt-blog/js/local-search.js" ></script>



  
    <script  src="/xt-blog/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/xt-blog/js/boot.js" ></script>


</body>
</html>
